//
//  FWEssenceCell.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/21.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWEssenceCell.h"
#import "FWTopicFrameModel.h"
#import "FWTopicModel.h"
#import "FWContentImgView.h"
#import "FWContentVideoView.h"
#import "FWContentVoiceView.h"
#import "FWTopComment.h"
#import "FWUser.h"
#import "UIImageView+Extension.h"

@interface FWEssenceCell ()
/** 头像 */
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
/** 昵称 */
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
/** 时间 */
@property (weak, nonatomic) IBOutlet UILabel *createdAtLabel;
/** 文字 */
@property (weak, nonatomic) IBOutlet UILabel *text_label;
/** “踩”按钮 */
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
/** “分享”按钮 */
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
/** “评论”按钮 */
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
/** “顶”按钮 */
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
/** 是否位新浪会员 */
@property (weak, nonatomic) IBOutlet UIImageView *vipView;

@property (weak, nonatomic) FWContentImgView *contentImgView;

@property (weak, nonatomic) FWContentVideoView *contentVideoView;

@property (weak, nonatomic) FWContentVoiceView *contentVoiceView;

@property (weak, nonatomic) IBOutlet UIView *topComView;
@property (weak, nonatomic) IBOutlet UILabel *topComLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topComH;
@end

@implementation FWEssenceCell

+ (instancetype)essenceCell{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}

- (FWContentImgView *)contentImgView{
    if (_contentImgView == nil) {
        FWContentImgView *contentImgView = [FWContentImgView contentImgView];
        [self.contentView addSubview:contentImgView];
        _contentImgView = contentImgView;
    }
    return _contentImgView;
}

- (FWContentVideoView *)contentVideoView{
    if (_contentVideoView == nil) {
        FWContentVideoView *contentVideoView = [FWContentVideoView contentVideoView];
        [self.contentView addSubview:contentVideoView];
        _contentVideoView = contentVideoView;
    }
    return _contentVideoView;
}

- (FWContentVoiceView *)contentVoiceView{
    if (_contentVoiceView == nil) {
        FWContentVoiceView *contentVoiceView = [FWContentVoiceView contentVoiceView];
        [self.contentView addSubview:contentVoiceView];
        _contentVoiceView = contentVoiceView;
    }
    return _contentVoiceView;
}


- (void)setModelF:(FWTopicFrameModel *)modelF{
    _modelF = modelF;
    FWTopicModel *model = _modelF.topic;
    self.nameLabel.text = model.name;
    self.createdAtLabel.text = model.created_at;
    self.text_label.text = model.text;
    [self.iconView fw_setImage:[NSURL URLWithString:model.profile_image]];
    
    self.vipView.hidden = !model.isSina_v;
    
    [self setupButton:self.dingButton count:model.love placeholder:@"顶"];
    [self setupButton:self.caiButton count:model.hate placeholder:@"踩"];
    [self setupButton:self.shareButton count:model.repost placeholder:@"分享"];
    [self setupButton:self.commentButton count:model.comment placeholder:@"评论"];
    
    if (model.type == EssenceTypePicture) {
        self.contentImgView.frame = _modelF.contentFrame;
        self.contentImgView.model = model;
        self.contentImgView.hidden = NO;
        self.contentVideoView.hidden = YES;
        self.contentVoiceView.hidden = YES;
    }else if (model.type == EssenceTypeVideo){
        self.contentVideoView.frame = _modelF.contentFrame;
        self.contentVideoView.model = model;
        self.contentVideoView.hidden = NO;
        self.contentImgView.hidden = YES;
        self.contentVoiceView.hidden = YES;
    }else if (model.type == EssenceTypeVoice){
        self.contentVoiceView.frame = _modelF.contentFrame;
        self.contentVoiceView.model = model;
        self.contentVoiceView.hidden = NO;
        self.contentImgView.hidden = YES;
        self.contentVideoView.hidden = YES;
    } else {
        self.contentImgView.hidden = YES;
        self.contentVideoView.hidden = YES;
        self.contentVoiceView.hidden = YES;
    }
    
    FWTopComment *topComment = _modelF.topic.top_cmt.firstObject;
    if (topComment) {
        NSString *topCommentStr = [NSString stringWithFormat:@"%@：%@", topComment.user.username, topComment.content];
        self.topComLabel.text = topCommentStr;
        self.topComView.hidden = NO;
        self.topComH.constant = _modelF.topCommentH;
    }else{
        self.topComView.hidden = YES;
    }
}

- (void)setFrame:(CGRect)frame{
    frame.size.height = _modelF.cellHeight - TBMARGIN;
    [super setFrame:frame];
}

- (void)setupButton:(UIButton *)button count:(int)count placeholder:(NSString *)placeholder{
    if (count > 10000) {
        placeholder = [NSString stringWithFormat:@"%.1f", count / 10000.0];
        placeholder = [placeholder stringByReplacingOccurrencesOfString:@".0" withString:@""];
    } else if (count != 0) {
        placeholder = [NSString stringWithFormat:@"%d", count];
    }
    [button setTitle:placeholder forState:UIControlStateNormal];
}

- (IBAction)onClickTopComBtn {
    UIAlertController *sheetVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *collecAction = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *handelAction = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:nil];
    [sheetVC addAction:cancelAction];
    [sheetVC addAction:collecAction];
    [sheetVC addAction:handelAction];
    
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    [vc presentViewController:sheetVC animated:YES completion:nil];
}


@end
